package com.icbc.controllers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.icbc.model.Document;
import com.icbc.services.DocumentService;

@Controller
@RequestMapping("/document")
public class DocumentController {

	@Autowired
	public DocumentService documentService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView showDocuments(ModelMap model){
		ArrayList<Document> documents = (ArrayList<Document>) documentService.getAllDocuments();
		model.addAttribute("documents", documents);
	    return new ModelAndView("document/index",model);
	}
	
	@RequestMapping(value="/new", method=RequestMethod.GET)
	public ModelAndView showForm(ModelMap model){
		Document doc = new Document();
		model.addAttribute("document", doc);
	    return new ModelAndView("document/new",model);
	}
	
	@RequestMapping(method=RequestMethod.POST)
    public String processForm(@ModelAttribute(value="document") Document doc,BindingResult result){
        if(result.hasErrors()){
            return "document/new";
        }else{
        	
        	Collection<String> tagsCollection = new ArrayList<String>();
        	for (Iterator<String> iterator = doc.getTags().iterator(); iterator.hasNext();) {
				String text = (String) iterator.next().toLowerCase();
	        	tagsCollection.add(text.trim());
			}
        	
        	doc.setTags(tagsCollection);
        	doc.setFecha(new Date());
        	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            String name = auth.getName(); //get logged in username
            doc.setUser(name);
        	//para nullear el id cuando es new, sino va por edit
        	if(doc.getId().equals("")){
        		doc.setId(null);
        	}
        	documentService.saveDocument(doc);
            return "redirect:document/edit/"+doc.getId();
        }
    }
	
	@RequestMapping(value="/detail/{id}", method=RequestMethod.GET)
    public String documentDetail(@PathVariable("id") String id, ModelMap model){
        Document doc = documentService.getDocumentById(id);
        model.addAttribute("document", doc);
		return "document/detail";
    }
	
	@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
    public ModelAndView documentEdit(@PathVariable("id") String id, ModelMap model){
        Document doc = documentService.getDocumentById(id);
        model.addAttribute("document", doc);
		return new ModelAndView("document/new",model);
    }
	
	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
    public String documentDelete(@PathVariable("id") String id){
        documentService.deleteDocumentById(id);
		return "redirect:/document";
    }
}